import time

from django.db import models
from DjangoUeditor.models import UEditorField
from UserProfile.models import User
# Create your models here.
class Type(models.Model): # 导航
    is_status = (
        (0, '否'),
        (1, '是')
    )
    name = models.CharField(verbose_name="类型名称", max_length=200)
    sort = models.CharField(verbose_name="排序", max_length=200,default=0)
    status = models.IntegerField(choices=is_status,verbose_name="状态",default=1)
    ctime = models.DateTimeField(verbose_name="创建时间",auto_now=True)
    class Meta:
        verbose_name = u'新闻资讯类型'
        verbose_name_plural = u"新闻资讯类型"
    def __str__(self):
        return self.name


# 内容
class News(models.Model):
    is_status = (
        (0, '否'),
        (1, '是')
    )
    user = models.ForeignKey(User,verbose_name="发布者",max_length=250,on_delete=models.CASCADE)
    type = models.ForeignKey(Type,verbose_name="类型id", max_length=250,on_delete=models.CASCADE)
    title = models.CharField(verbose_name="新闻标题", max_length=250)
    cover = models.ImageField(verbose_name='封面', upload_to='cover/{}/'.format(int(time.time())),default='default/blog.jpg', blank=True, null=True)
    is_recommend = models.IntegerField(choices=is_status,verbose_name="是否为推荐",default=0)
    content = UEditorField(width=750, height=300, toolbars="full", imagePath="images/news/{}/".format(int(time.time())), filePath="files/news/{}/".format(int(time.time())), upload_settings={"imageMaxSize": 5242880}, settings={}, verbose_name='新闻内容')
    # content = UEditorField(width=750, height=300, toolbars="full", imagePath="images/", filePath="files/", upload_settings={"imageMaxSize":1204000},settings={}, verbose_name='博客内容')
    source=models.CharField(verbose_name="来源",max_length=250)
    view = models.IntegerField(verbose_name="浏览量",default=0)
    comments = models.IntegerField(verbose_name="评论数",default=0)
    goods = models.IntegerField(verbose_name="点赞数",default=0)
    status = models.IntegerField(choices=is_status,verbose_name="状态", default=1)
    ctime = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)
    uptime = models.DateTimeField(verbose_name="修改时间",auto_now=True)
    class Meta:
        verbose_name = u'新闻'
        verbose_name_plural = u"新闻"

    def __str__(self):
        return self.title

class NComment(models.Model): # 评论表
    news = models.ForeignKey(News, verbose_name="新闻id", max_length=250, on_delete=models.CASCADE,)
    user = models.ForeignKey(User,verbose_name="用户id", max_length=250, on_delete=models.CASCADE)
    post = models.ForeignKey('self',verbose_name="回复id" ,null = True, blank = True ,on_delete=models.CASCADE)
    topic = models.IntegerField(blank = True ,default=0)
    content = UEditorField(width=750, height=300, toolbars="full", imagePath="/media/images/", filePath="/media/files/",
                           upload_settings={"imageMaxSize": 5242880}, settings={}, verbose_name='评论内容')
    ctime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    class Meta:
        verbose_name = u'评论管理'
        verbose_name_plural = u"评论管理"


class NGood(models.Model): #点赞表
    news = models.ForeignKey(News, verbose_name="新闻id", max_length=250, on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name="用户id", max_length=250, on_delete=models.CASCADE)
    ctime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    class Meta:
        verbose_name = u'点赞管理'
        verbose_name_plural = u"点赞管理"